package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.AEBaseQuery;
import com.deyuanyun.pic.ae.controller.dto.DifficultyRepairEdit;
import com.deyuanyun.pic.ae.controller.dto.SewerQuery;
import com.deyuanyun.pic.ae.domain.DifficultyRepair;
import com.deyuanyun.pic.ae.domain.DifficultyRepairExample;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.domain.SewerExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.mapper.DifficultyRepairMapper;
import com.deyuanyun.pic.ae.mapper.SewerMapper;
import com.deyuanyun.pic.ae.mapper.dao.DifficultyRepairDao;
import com.deyuanyun.pic.ae.mapper.dao.SewerDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.DifficultyRepairService;
import com.deyuanyun.pic.ae.service.SewerService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.coordinates.BaiduResVO;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.util.PipeMgrUtil;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “难以维修接近” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class DifficultyRepairServiceImpl extends AEBaseService implements DifficultyRepairService {

    @Resource
    private DifficultyRepairMapper difficultyRepairMapper;
    @Resource
    private DifficultyRepairDao difficultyRepairDao;

    @Override
    public Page<DifficultyRepair> query(AEBaseQuery aeBaseQuery) {
        return difficultyRepairDao.query(aeBaseQuery);
    }

    @Override
    public DifficultyRepair find(String id) {
        return difficultyRepairMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(DifficultyRepair difficultyRepair, List<String> imgs) {

        // 难以维修接近，只有线性数据，没有点状数据
        Identification beginIdentification = PipeMgrUtil.queryIdentificationFlag(difficultyRepair.getBeginNagmInput());
        Asserts.assertLogic((beginIdentification==null), "起点附近测试桩:“"+difficultyRepair.getBeginNagmInput()+"”不存在，请重新输入");
        difficultyRepair.setBeginNagmInput(beginIdentification.getId());
        difficultyRepair.setBeginNagmNumberInput(beginIdentification.getSignNumber());
        Identification endIdentification = PipeMgrUtil.queryIdentificationFlag(difficultyRepair.getEndNagmInput());
        Asserts.assertLogic((endIdentification==null), "终点附近测试桩:“"+difficultyRepair.getEndNagmInput()+"”不存在，请重新输入");
        difficultyRepair.setEndNagmInput(endIdentification.getId());
        difficultyRepair.setEndNagmNumberInput(endIdentification.getSignNumber());

        // 计算起始绝对里程
        PositionObj beginPositionObj = getPositionInfo(difficultyRepair.getPipeLineId(), difficultyRepair.getBeginX(), difficultyRepair.getBeginY());
        if (beginPositionObj != null) {
            difficultyRepair.setBeginMileage(beginPositionObj.getMileage());
            difficultyRepair.setBeginNagm(beginPositionObj.getNagm());
            difficultyRepair.setBeginNagmNumber(beginPositionObj.getNagmNumber());
            difficultyRepair.setBeginNagmMileage(beginPositionObj.getNagmMileage());
        }
        // 计算终点绝对里程
        PositionObj endPositionObj = getPositionInfo(difficultyRepair.getPipeLineId(), difficultyRepair.getEndX(), difficultyRepair.getEndY());
        if (endPositionObj != null) {
            difficultyRepair.setEndMileage(endPositionObj.getMileage());
            difficultyRepair.setEndNagm(endPositionObj.getNagm());
            difficultyRepair.setEndNagmNumber(endPositionObj.getNagmNumber());
            difficultyRepair.setEndNagmMileage(endPositionObj.getNagmMileage());
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(difficultyRepair.getId()) ? difficultyRepair.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(difficultyRepair.getId())){
            DifficultyRepairExample difficultyRepairExample = new DifficultyRepairExample();
            difficultyRepairExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andCustomNumberEqualTo(difficultyRepair.getCustomNumber());
            int countNum = difficultyRepairMapper.countByExample(difficultyRepairExample);
            Asserts.assertLogic(countNum > 0, "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 已经存在，不能重复插入");

            difficultyRepair.setId(bizzId);
            difficultyRepair.setCreateDate(new Date());
            difficultyRepair.setCreateUser(realName);
            difficultyRepair.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            difficultyRepair.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return difficultyRepairMapper.insertSelective(difficultyRepair);
        }

        DifficultyRepair difficultyRepairObj = this.find(difficultyRepair.getId());
        Asserts.assertLogic(difficultyRepairObj == null, "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 不存在，不允许修改");
        Asserts.assertLogic(isAuditReleased(difficultyRepairObj.getDataStated()), "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 已审核通过,不允许编辑");
        if (isAuditDismissed(difficultyRepairObj.getDataStated())){
            difficultyRepair.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return difficultyRepairMapper.updateByPrimaryKeySelective(difficultyRepair);
    }

    @Override
    public int audit(DifficultyRepair difficultyRepair) {
        DifficultyRepair difficultyRepairObj = this.find(difficultyRepair.getId());
        Asserts.assertLogic(difficultyRepairObj == null, "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 不存在，不允许修改");
        Asserts.assertLogic(isAudit(difficultyRepairObj.getDataStated()), "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 已审核,不允许重复审核");
        return difficultyRepairMapper.updateByPrimaryKeySelective(difficultyRepair);
    }

    @Override
    public int delete(String id) {
        DifficultyRepair difficultyRepair = this.find(id);
        Asserts.assertLogic(difficultyRepair == null, "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 不存在，不允许删除");
        Asserts.assertLogic(isAuditReleased(difficultyRepair.getDataStated()), "编号为:“"+difficultyRepair.getCustomNumber()+"”难以维修接近 已审核通过,不能“删除”");
        difficultyRepair = new DifficultyRepair();
        difficultyRepair.setId(id);
        difficultyRepair.setDeleted(Short.valueOf("1"));
        difficultyRepair.setUpdateDate(new Date());
        difficultyRepair.setUpdateUser(realName);
        difficultyRepair.setUpdateRemark("删除");
        return difficultyRepairMapper.updateByPrimaryKeySelective(difficultyRepair);
    }

}
